package src;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import lucene.LuceneConstants;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;

public class IndexYahooFaq {

	/**
	 * @param args
	 */
	private static IndexWriter iwriter;
	public IndexYahooFaq(){
		try{
			Analyzer analyzer = new  StandardAnalyzer();
			iwriter = new IndexWriter(LuceneConstants.yahooindexdir, analyzer, true);
			iwriter.setMergeFactor(100);
		}
		catch(Exception e){
			e.printStackTrace();
			System.exit(-1);
		}
	}
	
	public void indexData(File infile){
		//System.out.println(infile.getName());
		//File infile = new File(filename);
		int docnum =0;
				try {
					BufferedReader reader = new BufferedReader(new FileReader(infile));
					String sentence = null;
					while((sentence = reader.readLine())!=null){
						if(sentence.length()!= 0){
							String question = sentence;
							String answer = "Not Available";
							this.index(question,answer,docnum);
							docnum++;
						}
					}
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}		
	}
	public void index(String question, String answer, int docnum){
		System.out.println("Question  "+question);
		System.out.println("Answer "+answer);
		System.out.println("Docnum "+docnum);
		
		try{
			Document document  = new Document();
			document.add(new Field("question", question,Field.Store.YES,Field.Index.TOKENIZED));
			document.add(new Field("answer",answer,Field.Store.YES,Field.Index.UN_TOKENIZED));
			document.add(new Field("docnum",Integer.toString(docnum),Field.Store.YES,Field.Index.UN_TOKENIZED));
			iwriter.addDocument(document);	
		}
		catch(Exception e){
			e.printStackTrace();
			System.exit(-1);
		}
	}
	
	public void indexAllFile(String dir){
		File file = new File(dir);
		String files[] = file.list();
		
		for(int i=0; i<files.length; i++){
			File tfile = new File(dir+files[i]);
			if(tfile.isFile()){
				indexData(tfile);
			}
		}			
	}
	
	public static void main(String[] args) {
		IndexYahooFaq iyf = new IndexYahooFaq();
		iyf.indexAllFile(LuceneConstants.yahoodatadir); 		
	}
}
